package com.hmkj.admin.controller.fqa;

import com.hmkj.admin.controller.BaseController;
import com.hmkj.admin.controller.GenericController;
import com.hmkj.admin.utils.AdminSessionHelper;
import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.entity.dtgrid.DtGrid;
import com.hmkj.common.utils.StringUtils;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.po.fqa.Fqa;
import com.hmkj.core.po.sysNotice.SystemNotice;
import com.hmkj.core.service.fqa.FqaService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping(BaseController.BASE_URI)
public class FqaJsonController extends GenericController<Integer, Fqa> {
    @Resource
    private FqaService fqaService;

    /**
     * 客户列表列表json数据
     *
     * @param dtGridPager
     * @return
     */
    @ResponseBody
    @RequestMapping("fqa/json/list")
    public DtGrid userListJson(String dtGridPager) throws Exception {
        return fqaService.getDtGridList(dtGridPager, SystemNotice.class);
    }

    /**
     * 新增常见问题
     *
     * @param fqa
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("fqa/json/add")
    @RequiresPermissions("sys:fqa:save")
    public ResultEntity addJson(Fqa fqa) throws Exception {
        checkModel(fqa);
        if (fqaService.selectCount(new Fqa(t -> {
            t.setTitle(fqa.getTitle());
        })) > 0) {
            throw new BussinessException("标题已存在");
        }
        fqa.setCreator(AdminSessionHelper.getCurrAdmin().getName());
        fqaService.insert(fqa);
        return ok();
    }

    /**
     * 编辑常见问题
     *
     * @param fqa
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("fqa/json/edit")
    @RequiresPermissions("sys:fqa:edit")
    public ResultEntity editJson(Fqa fqa) throws Exception {
        checkModel(fqa);
        Fqa fqa1 = checkPrimaryKey(fqa.getId());
        if (!fqa1.getTitle().equals(fqa.getTitle())) {
            if (fqaService.selectCount(new Fqa(t -> {
                t.setTitle(fqa.getTitle());
            })) > 0) {
                throw new BussinessException("标题已存在");
            }
        }
        fqa.setUpdator(AdminSessionHelper.getCurrAdmin().getName());
        fqaService.updateByPrimaryKeySelective(fqa);
        return ok();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "fqa/json/del")
    @RequiresPermissions("sys:fqa:del")
    public ResultEntity del(Integer id) throws Exception {
        Fqa fqa = checkPrimaryKey(id);
        fqaService.updateByPrimaryKeySelective(new Fqa(a -> {
            a.setId(fqa.getId());
            a.setDelFlag(Fqa.DELFLAG.DELETED.code);
        }));
        return ok();
    }

    private Fqa checkPrimaryKey(Integer id) throws Exception {
        if (null == id) {
            throw new BussinessException("参数有误");
        } else {
            Fqa fqa = fqaService.selectByPrimaryKey(id);
            if (null != fqa) {
                return fqa;
            } else {
                throw new BussinessException("参数有误");
            }
        }
    }

    private void checkModel(Fqa fqa) {
        if (!StringUtils.isNotBlank(fqa.getTitle())) {
            throw new BussinessException("标题不能为空");
        } else if (!StringUtils.isNotBlank(fqa.getCategoryId())) {
            throw new BussinessException("问题分类不能为空");
        } else if (!StringUtils.isNotBlank(fqa.getAnswer())) {
            throw new BussinessException("回答不能为空");
        } else if (fqa.getTitle().length() > 100) {
            throw new BussinessException("标题长度不能超过100个字符");
        } else if (fqa.getAnswer().length() > 1000) {
            throw new BussinessException("回答长度不能超过1000个字符");
        }
    }
}
